<?php
import( 'de.ceus-media.framework.krypton.core.DefinitionView' );
import( 'classes.view.panel.ProjectSelector' );
import( 'classes.logic.Project' );
import( 'classes.logic.Server' );
import( 'classes.logic.ServerProject' );
/**
 *	Option Views.
 *	@extends		Core_View
 *	@uses			View_Panel_PlatformSelector
 *	@author			Christian WÃ¼rker <Christian.Wuerker@CeuS-Media.de>
 *	@since			06.08.2007
 *	@version		0.1
 */
/**
 *	Option Views.
 *	@extends		Core_View
 *	@uses			View_Panel_PlatformSelector
 *	@author			Christian WÃ¼rker <Christian.Wuerker@CeuS-Media.de>
 *	@since			06.08.2007
 *	@version		0.1
 */
class View_Administration_Project extends Framework_Krypton_Core_DefinitionView
{
	private $nodeId;

	/**
	 *	Builds Car Views.
	 *	@access		public
	 *	@return		string
	 */
	public function buildContent()
	{
		$request	= Framework_Krypton_Core_Registry::getStatic( 'request' );
		$session	= Framework_Krypton_Core_Registry::getStatic( 'session' );
//		$projectSelect	= new View_Panel_ProjectSelector();
//		$ui['content']	= $projectSelect->buildContent();
		
		$projectId		= $request->get( 'projectId' );
		
		if( $projectId )
		{
			$ui['content']	= $this->buildContentEdit( $projectId );
			$ui['content']	.= $this->buildContentServers( $projectId );
		}
		else
		{
			$ui['content']	= $this->buildContentList();
			$ui['content']	.= $this->buildContentAdd();
		}
		return $ui['content'];
		return $this->loadTemplate( 'administration.project.content', $ui );
	}

	private function buildContentAdd()
	{
		$request	= Framework_Krypton_Core_Registry::getStatic( 'request' );
		$session	= Framework_Krypton_Core_Registry::getStatic( 'session' );
		$words		= $this->words['project']['addProject'];
		
		$logic		= new Logic_Project();
		$opt_project	= $logic->getProjects();
		$sources	= array( 'opt_project'	=> $opt_project ); 
		
		//  --  UI COMPONENTS  --  //
		$ui	= $this->buildForm( 'administration/project', 'addProject', 'project', 'addProject', array(), $sources );
		$button_add		= UI_HTML_Elements::Button( 'addProject', $words['button_add'], 'but add' );
		$ui['field_button']	= UI_HTML_Elements::Field( '', $button_add );

		$ui['form']		= UI_HTML_Elements::Form( 'addProject', "?link=".$request->get( 'link' ) );
		$ui['caption']	= UI_HTML_Elements::TableCaption( $words['caption'], 'add' );
		$ui['colgroup']	= UI_HTML_Elements::ColumnGroup( $words['colgroup'] );
		return $this->loadTemplate( 'administration.project.add', $ui );
	}

	private function buildContentEdit( $projectId )
	{
		$request	= Framework_Krypton_Core_Registry::getStatic( 'request' );
		$session	= Framework_Krypton_Core_Registry::getStatic( 'session' );
		$words		= $this->words['project']['editProject'];
		
		$logic	= new Logic_Project();
		$data	= $logic->getProjectData( $projectId );

		//  --  UI COMPONENTS  --  //
		$ui	= $this->buildForm( 'administration/project', 'editProject', 'project', 'editProject', $data );
		$button_cancel	= UI_HTML_Elements::LinkButton( '?link='.$request->get( 'link' ), $words['button_cancel'], 'but cancel' );
		$button_edit	= UI_HTML_Elements::Button( 'editProject', $words['button_edit'], 'but edit' );
		$ui['field_button_cancel']	= UI_HTML_Elements::Field( '', $button_cancel );
		$ui['field_button_edit']	= UI_HTML_Elements::Field( '', $button_edit );

		$ui['form']		= UI_HTML_Elements::Form( 'editProject', "?link=".$request->get( 'link' )."&projectId=".$projectId );
		$ui['caption']	= UI_HTML_Elements::TableCaption( $words['caption'], 'edit' );
		$ui['colgroup']	= UI_HTML_Elements::ColumnGroup( $words['colgroup'] );
		return $this->loadTemplate( 'administration.project.edit', $ui );
	}

	private function buildContentServers( $projectId )
	{
		$request	= Framework_Krypton_Core_Registry::getStatic( 'request' );
		$session	= Framework_Krypton_Core_Registry::getStatic( 'session' );
		$words		= $this->words['project']['assignServers'];
		
		$servers	= Logic_Server::getServers( true );
		$relations	= Logic_ServerProject::getServersFromProject( $projectId, false );
		$add	= $remove	= array();
		foreach( $servers as $serverId => $serverTitle )
		{
			
			if( in_array( $serverId, $relations ) )
				$remove[$serverId]	= $serverTitle;
			else
				$add[$serverId]	= $serverTitle;
		}
		$sources	= array(
			'opt_add_servers'		=> $add,
			'opt_remove_servers'	=> $remove,
		);

		//  --  UI COMPONENTS  --  //
		$ui	= $this->buildForm( 'administration/project', 'assignServers', 'project', 'assignServers', array(), $sources );
		$button_add		= UI_HTML_Elements::Button( 'addServers', $words['button_add'], 'but add' );
		$button_remove	= UI_HTML_Elements::Button( 'removeServers', $words['button_remove'], 'but remove' );
		$ui['field_button_add']		= UI_HTML_Elements::Field( '', $button_add );
		$ui['field_button_remove']	= UI_HTML_Elements::Field( '', $button_remove );

		$ui['form']		= UI_HTML_Elements::Form( 'assignServers', "?link=".$request->get( 'link' )."&projectId=".$projectId );
		$ui['caption']	= UI_HTML_Elements::TableCaption( $words['caption'], 'edit' );
		$ui['colgroup']	= UI_HTML_Elements::ColumnGroup( $words['colgroup'] );
		return $this->loadTemplate( 'administration.project.servers', $ui );
	}

	private function buildContentList()
	{
		$request	= Framework_Krypton_Core_Registry::getStatic( 'request' );
		$link		= $request->get( 'link' );
		$projects	= Logic_Project::getProjectList();
		$ui		= $this->words['project']['listProjects'];
		$list	= array();
		foreach( $projects as $project )
		{
			$servers	= Logic_ServerProject::getServersFromProject( $project['project_id'], 2 );
			foreach( $servers as $serverId => $serverData )
			{
				$title		= UI_HTML_Elements::Acronym( $serverData['title'], $serverData['description'] );
				$servers[$serverId]	= UI_HTML_Elements::Link( "?link=manageServers&serverId=".$serverId, $title );
			}
			$project['servers']	= implode( "<br/>", array_values( $servers ) );
			$project['link']	= $link;
			$project['project']	= Logic_Project::getProjectTitle( $project['project_id'] );
			$project['created']	= $project['created'] ? date( "d.m.Y H:i", $project['created'] ) : "";
			$project['modified']	= $project['modified'] ? date( "d.m.Y H:i", $project['modified'] ) : "";
			$list[]	= $this->loadTemplate( 'administration.project.item', $project );
		}
		$ui['list']	= implode( "\n", $list );
		return $this->loadTemplate( 'administration.project.list', $ui );
	}
}
?>
